package com.hq.shop04user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hq.shop04user.entity.User;
import com.hq.shop04user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("addUser")
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);
    }

    @GetMapping("getList")
    public List<User> getList() {
        return userService.list();
    }

    @PostMapping("queryList")
    public List<User> queryList(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(user.getUsername())) {
            queryWrapper.like("username", user.getUsername());
        }
        if (!StringUtils.isEmpty(user.getEmail())) {
            queryWrapper.like("email", user.getEmail());
        }
        if (!StringUtils.isEmpty(user.getPhone())) {
            queryWrapper.like("phone", user.getPhone());
        }
        return userService.list(queryWrapper);
    }

    @GetMapping("getUserByName")
    public User getUserByName(@RequestParam("username") String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", name);
        return userService.getOne(wrapper);
    }

    @GetMapping("/getUserById")
    public User getUserById(String uuid) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("auth_id", uuid);
        return userService.getOne(wrapper);
    }
}
